package com.treeman.navi;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.treeman.navi.friends.Buffer;
import com.treeman.navi.friends.FriendActivity;
import com.treeman.navi.login.ApiRequestError;
import com.treeman.navi.login.Session;
import com.treeman.navi.rest.RestClient;
import com.treeman.navi.rest.RestClient.HttpConnectionStatus;
import com.treeman.navi.rest.RestClient.RequestMethod;
import com.treeman.navi.user.User;

public class FriendsActivity extends ListActivity {
	protected static final String TAG = "FriendsActivity";
	private List<User> friendsList;
;
	
//	public List<String> titles = new ArrayList<String>(friendsList.size());

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.friends);
		downloadFriends();
		
//		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//				R.layout.row2, titles);
//		this.setListAdapter(adapter);
		// Use your own layout
		
		
	}

	
	 private void downloadFriends()
	    {
		Handler handler = new Handler()
		{

		    @Override
		    public void handleMessage(Message msg)
		    {
			if (msg.what == HttpConnectionStatus.START.ordinal())
			{
			    Log.i(TAG, "Starting connection. Downloading friends list. ");

			    try
			    {

			    }
			    catch (Exception ex)
			    {
				Log.e(TAG, " START error : ", ex);
			    }
			}
			if (msg.what == HttpConnectionStatus.SUCCESS.ordinal())
			{
			    Log.i(TAG, "Connection success! Download friends list.");

			    try
			    {

				RestClient restClient = (RestClient) msg.obj;

				String response = restClient.getResponse();

				Log.i(TAG, "Response: " + response + "( RESPONSE CODE " + restClient.getResponseCode() + ")");

				Gson gson = new Gson();
				Type listType = new TypeToken<List<User>>()
				{
				}.getType();

				try
				{
					friendsList = gson.fromJson(response, listType);
					 List<String> titles = new ArrayList<String>(friendsList.size());
					 
					

					for (User usr : friendsList) {
						titles.add(usr.getUsername() + "\n" + usr.getFirstName() + " " + usr.getLastName());
						

					}
					
			    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(FriendsActivity.this,
							R.layout.row2, R.id.LoginLabel, titles);
			    	

					FriendsActivity.this.setListAdapter(adapter);
					
	
					Log.d(TAG, "Deserialization Succes." + friendsList.size());
				}
				
				catch (JsonSyntaxException ex)
				{
				    Log.e(TAG, "JSON syntax exception. Probably wrong data.");
				}

				

			    }
			    
			    catch (Exception ex)
			    {
				Log.e(TAG, " SUCCESS error : ", ex);
			    }

			    
			}
			if (msg.what == HttpConnectionStatus.ERROR.ordinal())
			{
			    Log.i(TAG, "Connection error while downloading friends list!");
			    try
			    {
				ApiRequestError apiRequestError = (ApiRequestError) msg.obj;
				Log.i(TAG, "Response: " + apiRequestError.getReason());

			    }
			    catch (Exception ex)
			    {
				Log.e(TAG, " CONNECTION error : ", ex);
			    }
			}
		    }
		};

		// CONNECTION
		String url = Session.getAddress() + "/api/me/friends";
		RestClient restClient = new RestClient(url, handler);
		try
		{
		    restClient.execute(RequestMethod.GET);
		}
		catch (Exception ex)
		{
		    Log.e(TAG, " RestClient error : ", ex);
		}
	    }


	@Override

	
	public void onListItemClick(ListView parent, View view, int position,
			long id) {
		
		
//		Log.d(marker, "" + tab[position] + " " + position + " " + inc);
		Buffer.setUsername(friendsList.get(position).getUsername());
		Buffer.setFirstname(friendsList.get(position).getFirstName());
		Buffer.setLastname(friendsList.get(position).getLastName());
		Buffer.setEmail(friendsList.get(position).getEmail());
		Intent o = new Intent(this, FriendActivity.class);
		startActivity(o);
//		Toast.makeText(this, "Wybrano: " + Buffer.getUsername() , Toast.LENGTH_SHORT).show();
	}



}
